exports.answerDetail = function (req, res) {
//问题内容页
    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;
    //最终结果
    var resultData = {};

    var limit = restfulParams.limit == undefined ? config.publicConfig.LIMIT : restfulParams.limit;

    var skip = restfulParams.skip == undefined ? 0 : restfulParams.skip;

    var answerData = 0;
    //任务流创建
    async.waterfall([
        function (cb) {
          resultData.answer_commentData = [];
          cb()
        },
        //获取问题
        function (cb) {

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.search_content + "/fc_id/" + restfulParams.fc_id,
                    method: 'POST',
                    data: {
                        fields: [
                            "fc_id",
                            "fc_fcid",
                            "fc_name",
                            "fc_uid",
                            "fc_title",
                            "fc_content",
                            "fc_images",
                            "fc_answer_count",

                        ].join(",")
                    }

                },
                function (err, data) {

                    resultData.questionData = data.data;

                    cb();
                }
            );
        },

        //获取总的回答数
        function (cb) {

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.search_answer_userinfo + "/fcc_fcid/" + restfulParams.fc_id + "/count/1",
                    method: 'POST',
                    data: {
                        fields: [
                            "fca_id"
                        ].join(",")
                    }

                },
                function (err, data) {

                    resultData.answerCount = data.count;

                    cb();
                }
            );

        },

        //获取回答
        function (cb) {

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.search_answer_userinfo + "/fca_id/" + restfulParams.fca_id,
                    method: 'POST',
                    data: {
                        fields: [
                            "fca_id",
                            "fca_content",
                            "fca_images",
                            "fca_zan_count",
                            "fca_collect_count",
                            "fca_share_count",
                            "fca_comment_count",
                            "fca_create_time",
                            "u_nickname",
                            "u_sign",
                            "u_type",
                            "u_header_url",
                            "u_id",
                            "u_oid"
                        ].join(",")
                    }

                },
                function (err, data) {

                    resultData.answerData = data.data;

                    answerData = data.data;
                    cb();
                }
            );
        },
        function (cb) {

            // var answerData = resultData.answerData[0];

            if (restfulParams.o_uid) {

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchOrganization + "/o_uid/" + restfulParams.o_uid,
                        method: 'POST',
                        data: {
                            fields: ["o_name", "o_logo"].join(",")
                        }
                    },
                    function (err, data) {
                        var organizationInfo = data.data[0];
                        answerData[0].o_name = organizationInfo.o_name;
                        answerData[0].o_logo = organizationInfo.o_logo;
                        answerData[0].isOrganization = 1;
                        cb();
                    }
                );
            } else {
                answerData[0].isOrganization = 0;
                cb()
            }
        },

        /*function (cb) {

            var answerData = resultData.answerData;

            var tasks = [];

            answerData.forEach(function (item, i) {

                tasks.push(function (cb) {

                    if (item.u_type == 1) {
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.searchOrganization + "/o_id/" + item.u_oid,
                                method: 'POST',
                                data: {
                                    fields: ["o_name", "o_logo"].join(",")
                                }
                            },
                            function (err, data) {

                                item.o_name = data.data[0].o_name;

                                item.o_logo = data.data[0].o_logo;

                                cb();
                            }
                        );
                    } else {
                        item.o_name = null;

                        item.o_logo = null;
                        cb()
                    }
                })
            });
            async.waterfall(tasks, cb);
        },*/

        //查询机构的约课信息

        function (cb) {

            if (answerData[0].u_oid > 0) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.search_organization_course + "/oc_oid/" + answerData[0].u_oid + "/oc_type/1",
                        method: 'POST',
                        data: {
                            fields: ["oc_id", "oc_title", "oc_course_count", "oc_new_price"].join(",")
                        }

                    },
                    function (err, data) {

                        //console.log(data);

                        resultData.courseData = data.data;

                        cb();
                    }
                );
            } else {
                resultData.courseData = [];
                cb();
            }
        },
        /*//如果u_type = 1 时， 是机构角色，查询机构信息
        function (cb) {
            var answerData = resultData.answerData;
            //创建添加任务
            var tasks = [];

            //循环节点列表
            answerData.forEach(function (item, i) {

                //创建每一个插入任务
                tasks.push(function (cb) {
                    if(item.u_type==1){
                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.searchOrganization + "/is_delete/0/o_id/" + item.u_oid ,
                                method: 'POST',
                                data: {
                                    fields: ["o_id","o_name","o_logo"].join(",")
                                }

                            },
                            function (err, data) {

                                item.o_id = data.o_id;
                                item.o_name = data.o_name;
                                item.o_logo = data.o_logo;
                                cb();
                            });
                    }else{
                        cb();
                    }

                });
            });
            async.waterfall(tasks, cb);

        },*/
        //判断是否关注回答问题的人
        function (cb) {


            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.searchMasterConcern + "/count/1/is_delete/0/mc_master_id/" + resultData.answerData[0].u_id + "/mc_uid/" + restfulParams.u_id,
                    method: 'GET',
                },
                function (err, data) {

                    //console.log(data);

                    var answerData = data.data;

                    if (answerData.length > 0) {
                        resultData.isConcern = 1;
                    } else {
                        resultData.isConcern = 0;
                    }


                    cb();
                }
            );
        },


        //查询当前用户是否点过赞
        function (cb) {
            if (restfulParams.u_id) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.searchContentAnswerZanRecord + "/is_delete/0/fcazr_fcaid/" + restfulParams.fca_id + "/fcazr_uid/" + restfulParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ['fcazr_id'].join(",")
                        }

                    },
                    function (err, data) {

                        var is_zan = data.data;

                        if (is_zan <= 0) {
                            resultData.isZanData = 0;
                        } else {
                            resultData.isZanData = 1;
                        }
                        cb();
                    }
                );
            } else {
                cb();
            }

        },

        //查询当前用户是否已收藏该回答
        function (cb) {
            if (restfulParams.u_id) {
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.userSearchCollect + "/is_delete/0/uc_type/4/uc_fid/" + restfulParams.fca_id + "/uc_uid/" + restfulParams.u_id,
                        method: 'POST',
                        data: {
                            fields: ['uc_id'].join(",")
                        }

                    },
                    function (err, data) {

                        var is_zan = data.data;

                        if (is_zan <= 0) {
                            resultData.isCollectData = 0;
                        } else {
                            resultData.isCollectData = 1;
                        }
                        cb();
                    }
                );
            } else {
                cb();
            }

        }

    ], function (err, result) {

        //返回最终拼接结果
        RES.response(res, true, resultData);

    });

}